package gui.frames;

import gui.GuiManager;
import gui.GuiManager.FramesEnum;
import gui.components.SimpleInternalFrame;
import gui.panels.configurations.EntryPanel;
import gui.panels.configurations.EntryPanel.EntryMode;
import gui.panels.configurations.ImportPanel;
import gui.utils.ComponentUtils;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class ConfigurationsFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private SimpleInternalFrame managerFrame;

    public ConfigurationsFrame() {
    	setContentPane(buildContentPane());
        setTitle("Configurations");

        setSize(new java.awt.Dimension(640, 615));
        setResizable(false);
        ComponentUtils.centerOnScreen(this);

        ComponentUtils.installWindowClosingListener(this);
    }

    private Container buildContentPane() {
        SimpleInternalFrame operationsFrame = new SimpleInternalFrame(ComponentUtils.readImageIcon("operations.png"), "Operations");
        managerFrame = new SimpleInternalFrame(ComponentUtils.readImageIcon("manager.png"), "Configurations");
        JSplitPane splitPane = ComponentUtils.createStrippedSplitPane(JSplitPane.HORIZONTAL_SPLIT, operationsFrame, managerFrame, 1f);
        splitPane.setContinuousLayout(true);

        // Default panel:
        EntryPanel newEntryPanel = new EntryPanel(EntryMode.New);
        SwitchPanel(newEntryPanel);
        
        JButton newEntryButton = ComponentUtils.createButton("Create entry", "new_entry", true);
		newEntryButton.addActionListener(new SwitchPanelAction(newEntryPanel));

		JButton editEntryButton = ComponentUtils.createButton("Edit entry", "edit_entry", true);
		editEntryButton.addActionListener(new SwitchPanelAction(new EntryPanel(EntryMode.Edit)));
		
		JButton deleteEntryButton = ComponentUtils.createButton("Delete entry", "delete_entry", true);
		deleteEntryButton.addActionListener(new SwitchPanelAction(new EntryPanel(EntryMode.Delete)));
        
		JButton importButton = ComponentUtils.createButton("Import", "import", true);
        importButton.addActionListener(new SwitchPanelAction(new ImportPanel()));

		JButton backButton = ComponentUtils.createButton("Back", "stop", true);
        backButton.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent e) {
				GuiManager.switchFrame(FramesEnum.PREVIOUS_FRAME);
			}
		});

		Box buttonsBox = Box.createVerticalBox();
		buttonsBox.setOpaque(false);
		buttonsBox.add(newEntryButton);
		buttonsBox.add(editEntryButton);
		buttonsBox.add(deleteEntryButton);
		buttonsBox.add(importButton);
		buttonsBox.add(backButton);

		JScrollPane operations = new JScrollPane(buttonsBox);
		operations.setOpaque(false);
		operations.getViewport().setOpaque(false);
		operations.setViewportBorder(null);
        operationsFrame.setContent(operations);

        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.CENTER, splitPane);
        return pane;
    }
    
	private void SwitchPanel(JPanel panel) {
		managerFrame.setContent(panel);
		managerFrame.updateUI();
	}

    class SwitchPanelAction implements ActionListener {
    	private final JPanel panel;

		public SwitchPanelAction(JPanel panel) {
			this.panel = panel;
    	}
    	
		@Override
		public void actionPerformed(ActionEvent e) {
			SwitchPanel(panel);
		}  	
    }

}
